package ogb;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

/**
 * Tests the utility methods in DeaconBlues.java.
 * 
 * @author Branden Ogata
 *
 */
public class TestDeaconBluesTrigonometry {

  /**
   * Runs through some of the utility operations in DeaconBlues to ensure their validity.
   * 
   */
  
  @Test 
  public void testTrig() {
    DeaconBlues robot = new DeaconBlues();
    
    // First, assume that the robot is at (0,0) facing north (0 degrees).  The objective 
    // is to move to a point sqrt(8) distance away at a bearing of 45 degrees.  Although 
    // this robot could simply move there given the bearing and distance, it might be 
    // useful to have some knowledge of the coordinates before actually arriving.  
    double diffX = robot.calculatePointX(Math.sqrt(8), 45);
    double diffY = robot.calculatePointY(Math.sqrt(8), 45);
    
    // This forms a triangle of sorts:
    //     /|
    //    / |
    //   /  | diffY
    //  /   |
    // /    |
    // ------
    //  diffX
    // 
    // The two non-right angles of the triangle can be proven to be 45 degrees.
    // If this is correct, then diffX == diffY; JUnit can test that hypothesis.
    assertEquals("Compare diffX, diffY", diffX, diffY, 0.16);

    // The hypoteneuse of the triangle is sqrt(8); since a^2 + b^2 = c^2 for right triangles,
    // c = sqrt(a^2 + b^2).  If a == b though, then c = sqrt(2a^2).  c == sqrt(8), so 
    //   8 = 2a^2 
    //   4 = a^2
    //   2 = a
    // Thus diffX and diffY should both be 2, which JUnit can check.
    assertTrue("diffX, diffY == 2", ((diffX - 2.0 < 0.16) && (diffY - 2.0 < 0.16)));
    
    // DeaconBlues also has methods to calculate the bearing and distance based on coordinates.
    // Since these were given to start with, those values can help in verifying the correctness
    // of these other methods.
    assertEquals("Check bearing ", robot.calculateBearing(diffX, diffY), 45.0, 0.16);
    assertEquals("Check distance ", robot.calculateDistance(diffX, diffY), Math.sqrt(8), 0.16);
  }
  
}
